# Copyright (c) 2008 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that be
# found in the LICENSE file.
#

# simple Makefile for Native Client Quake demo
# (Linux, Mac & Cygwin)
#
# Please see googleclient/native_client/common/Makefile.mk for details
# TODO: remove dependency on native_client directory
#
# type:
#   make debug nacl run
# to build Quake and run it as a Native Client application.
#
# This file and the sources expect to be in googleclient/native_client/tests/quake

NAME:=naclquake
NACL_CPP:=nacl-gcc
STANDALONE_CPP:=gcc -lstdc++
CCFLAGS := -D_REENTRANT -DSDL -DELF -DPACKAGE=\"naclquake\" -DVERSION=\"1.0.9\" -I.

# if nacl target, copy nexe to "nacl_quake" for quake.html to launch
# and add nacl_file.c to list of files for browser support.
ifeq (nacl,$(filter nacl,$(MAKECMDGOALS)))
  CCFLAGS += -Xlinker --wrap -Xlinker read
  CCFLAGS += -Xlinker --wrap -Xlinker open
  CCFLAGS += -Xlinker --wrap -Xlinker close
  CCFLAGS += -Xlinker --wrap -Xlinker lseek
  POST_BUILD = cp $(EXE_NAME) nacl_quake
  FILES := nacl_file.c
else
  FILES :=
endif

ifeq (release,$(filter release,$(MAKECMDGOALS)))
  OPT := -O2
else
  OPT := -O0 -g
endif

X86_SRCS :=  snd_mixa.S sys_dosa.S d_draw.S d_draw16.S d_parta.S d_polysa.S\
             d_scana.S d_spr8.S d_varsa.S math.S r_aclipa.S r_aliasa.S\
             r_drawa.S r_edgea.S surf16.S surf8.S worlda.S

NONX86_SRCS :=  d_vars.c nonintel.c

FILES += cd_nacl.c chase.c cl_demo.c cl_input.c cl_main.c cl_parse.c\
         cl_tent.c cmd.c common.c console.c crc.c cvar.c d_edge.c d_fill.c\
         d_init.c d_modech.c d_part.c d_polyse.c d_scan.c d_sky.c d_sprite.c\
         d_surf.c d_zpoint.c draw.c host.c host_cmd.c keys.c mathlib.c menu.c\
         model.c net_bsd.c net_dgrm.c net_loop.c net_main.c net_udp.c\
         net_vcr.c net_wso.c pr_cmds.c pr_edict.c pr_exec.c r_aclip.c\
         r_alias.c r_bsp.c r_draw.c r_edge.c r_efrag.c r_light.c r_main.c\
         r_misc.c r_part.c r_sky.c r_sprite.c r_surf.c r_vars.c r_varsa.S\
         sbar.c screen.c snd_dma.c snd_mem.c snd_mix.c snd_nacl.c sv_main.c\
         sv_move.c sv_phys.c sv_user.c sys_nacl.c vid_nacl.c view.c wad.c\
         world.c zone.c $(X86_SRCS) $(NONX86_SRCS)
# These files were excluded from FILES because the use instructions
# disallowed by Native Client
OMITTED_NACL_FILES := d_copy.S dosasm.S

CLIENT:=$(filter-out *%,$(subst /native_client,/native_client *,$(CURDIR)))
include $(CLIENT)/common/Makefile.mk

.PHONY: download cleanall

QUAKE_URL=http://www.libsdl.org/projects/quake
download:
	$(CLIENT)/tools/download.sh $(QUAKE_URL)/src/sdlquake-1.0.9.tar.gz quake_src.tgz
	$(CLIENT)/tools/download.sh $(QUAKE_URL)/data/quakesw-1.0.6.tar.gz quake_data.tgz
	tar -x --strip-components=1 -f quake_src.tgz
	tar -x -f quake_data.tgz
	# apply Native Client patch
	patch -p1 <nacl_quake.patch
	# clean up - remove the tarballs
	rm -rf quake_src.tgz
	rm -rf quake_data.tgz


allclean: clean
	# clean out source except for nacl_file.c
	find . -name "*.c" !  -name "nacl_file.c"   -exec "rm" "{}" ";"
	# remove all headers and other stuff, if any
	rm -f  ./*.h  ./*.S WinQ*  *.bat conf*
